﻿#include "SubWindowManager.h"
#include "SARibbonMWUi.h"
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QList>
#include <QToolBar>
#include "MainWidgets/preWindow.h"
#include "MainWidgets/ControlPanel.h"
#include "MeshData/meshSingleton.h"
#include "Geometry/geometryData.h"
#include "PostWidgets/PostWindowBase.h"
#include "PostWidgets/RealTimeWindowBase.h"
#include "ModuleBase/graphWindowBase.h"
#include "MainWindow.h"
#include "signalHandler.h"
#include "PythonModule/PyAgent.h"
#include "GeometryCommand/GeoCommandPy.h"
#include "SARibbonBar/SARibbonBar.h"
#include "PostInterface/PostProcessPy.h"
#include "Common/Types.h"
#include <QDebug>
#include <assert.h>
#include "MainWidgets/C3fdRenderWindow.h"

namespace GUI
{
	SubWindowManager::SubWindowManager(MainWindow *mainwindow, QMdiArea *mdiArea, SignalHandler *hander, MainWidget::ControlPanel *cp)
		: _mainWindow(mainwindow), _mdiArea(mdiArea), _signalHander(hander), _controlPanel(cp)
	{
		connect(mainwindow, SIGNAL(closePreWindowSig()), this, SLOT(closePreWindow()));
		connect(mainwindow, SIGNAL(closeC3fdWindowSig()), this, SLOT(closeC3fdWindow()));
		connect(mainwindow, SIGNAL(openPostWindowSig(Post::PostWindowBase *)), this, SLOT(openPostWindow(Post::PostWindowBase *)));
		connect(mainwindow, SIGNAL(openRealTimeWindowSig(Post::RealTimeWindowBase *, int)), this, SLOT(openRealTimeWindow(Post::RealTimeWindowBase *)));
		connect(mainwindow, SIGNAL(showPostWindowInfoSig(int, int)), this, SLOT(showPostWindowInfo(int, int)));
		connect(mainwindow, SIGNAL(closePostWindowSig(Post::PostWindowBase *)), this, SLOT(closePostWindow(Post::PostWindowBase *)));
		connect(_mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow *)), this, SLOT(subWindowActived(QMdiSubWindow *)));
		connect(mainwindow, SIGNAL(closeRealTimeWindowSig(Post::RealTimeWindowBase *)), this, SLOT(closeRealTimeWindow(Post::RealTimeWindowBase *)));

		connect(mainwindow, SIGNAL(saveImageSig(QString, int, Post::PostWindowBase *, int, int)), this, SLOT(saveImage(QString, int, Post::PostWindowBase *, int, int)));
		connect(mainwindow, SIGNAL(saveImage(int, int, QString)), this, SLOT(saveImage(int, int, QString)));

		connect(this, SIGNAL(setViewSig(int, QString, QString)), this, SLOT(setView(int, QString, QString)));
		connect(this, SIGNAL(setViewValueSig(int, QString, int, int, int, int, int, int, int, int, int)), this, SLOT(setViewValue(int, QString, int, int, int, int, int, int, int, int, int)));
		connect(this, SIGNAL(saveImageSig(int, int, int, QString, QString)), this, SLOT(saveImage(int, int, int, QString, QString)));
		connect(this, SIGNAL(openPreWindowSig()), this, SLOT(openPreWindow()));
	}
	SubWindowManager::~SubWindowManager()
	{
	}
	void SubWindowManager::openPreWindow()
	{
		if (isPreWindowOpened())
		{
			Py::PythonAgent::getInstance()->unLock();
			return;
		}
		if (_preWindow.first == nullptr || _preWindow.second == nullptr)
		{
			_preWindow.second = new MainWidget::PreWindow(_mainWindow);
			_preWindow.first = _mdiArea->addSubWindow(_preWindow.second);
			_signalHander->updateActionsStates();
			Command::GeometryCommandPy::init(_mainWindow, _preWindow.second);
		}
		//		else
		updatePreActors();
		_preWindow.first->showMaximized();
		_preWindow.first->setFocus();
		_mdiArea->setActiveSubWindow(_preWindow.first);
		_mainWindow->getUi()->actionPre_Window->setEnabled(false);
		emit _mainWindow->preWindowOpenedSig(_preWindow.second);
		Py::PythonAgent::getInstance()->unLock();
	}

	void SubWindowManager::openGeometryWindow()
	{
		// if (_geometryWindow.first == nullptr || _geometryWindow.second == nullptr)
		//{
		//	_geometryWindow.second = new GenerateGeometry::GenerateGeometryWidget(_mainWindow);
		//	_geometryWindow.first = _mdiArea->addSubWindow(_geometryWindow.second);
		// }
		//_preWindow.first->showMaximized();
		//_preWindow.first->setFocus();
	}

	QMdiArea *SubWindowManager::getMdiArea() const
	{
		return _mdiArea;
	}

	void SubWindowManager::openC3fdWindow()
	{ /*
		 if (isC3fdWindowOpened())
		 {
			 return;
		 }*/
		if (_c3fdRenderWindow.first == nullptr || _c3fdRenderWindow.second == nullptr)
		{
			_c3fdRenderWindow.second = new MainWidget::C3fdRenderWindow(_mainWindow);
			_c3fdRenderWindow.first = _mdiArea->addSubWindow(_c3fdRenderWindow.second);
		}

		_c3fdRenderWindow.first->showMaximized();
		_c3fdRenderWindow.first->setFocus();
		_mdiArea->setActiveSubWindow(_c3fdRenderWindow.first);
		_mainWindow->getUi()->actionC3FD_Window->setEnabled(false);
	}

	void SubWindowManager::closeC3fdWindow()
	{
		if (_c3fdRenderWindow.second != nullptr)
		{
			if (_c3fdRenderWindow.first != nullptr)
			{
				_mdiArea->removeSubWindow(_c3fdRenderWindow.second);
				delete _c3fdRenderWindow.first;
				_c3fdRenderWindow.first = nullptr;
			}
			delete _c3fdRenderWindow.second;
			_c3fdRenderWindow.second = nullptr;
		}
		_mainWindow->getUi()->actionC3FD_Window->setEnabled(true);
	}

	MainWidget::C3fdRenderWindow *SubWindowManager::getC3fdRenderWindow() {
		return _c3fdRenderWindow.second;
	}


	void SubWindowManager::renderC3fdGeom(QStringList files, bool showHubAndShroud)
	{
		openC3fdWindow();
		_c3fdRenderWindow.second->renderC3fdGeom(files, showHubAndShroud);
	}
	void SubWindowManager::renderC3fdMesh(QStringList files)
	{
		openC3fdWindow();
		_c3fdRenderWindow.second->renderC3fdMesh(files);
	}

	void SubWindowManager::updateC3fdLineChart(QString monitorFile)
	{
		openC3fdWindow();
		//_c3fdRenderWindow.second->renderC3fdLineChart(monitorFile);
		_c3fdRenderWindow.second->renderC3fdMonitorCurve(monitorFile);
	}

	void SubWindowManager::updateC3fdPerformCurve(QList<QList<double>> totalData, QList<QList<double>> perStageData)
	{
		openC3fdWindow();
		_c3fdRenderWindow.second->renderC3fdPerformCurve(totalData, perStageData);
	}

	void SubWindowManager::renderC3fdPostData(QStringList files, QStringList types)
	{
		openC3fdWindow();
		_c3fdRenderWindow.second->renderC3fdPostData(files, types);
	}

	void SubWindowManager::add3dRenderPage(const QList<QToolBar *> &toolbars)
	{
		if (_threeD_render_page || toolbars.size() != 4)
			return;
		QList<QAction *> actions;
		QAction *action{};
		SARibbonBar *ribbon = _mainWindow->ribbonBar();
		_threeD_render_page = ribbon->addCategoryPage(QObject::tr("3D Render"));

		SARibbonPannel *pannel0 = _threeD_render_page->addPannel(QString());
		QToolBar *toolbar0 = toolbars.at(0);
		actions = toolbar0->actions();
		for (int j = 0; j < actions.size(); j++)
		{
			action = actions.at(j);
			pannel0->addLargeAction(action);
		}

		SARibbonPannel *pannel2 = _threeD_render_page->addPannel(QString());
		QToolBar *toolbar2 = toolbars.at(2);
		actions = toolbar2->actions();
		for (int j = 0; j < actions.size(); j++)
		{
			action = actions.at(j);
			pannel2->addLargeAction(action);
		}

		SARibbonPannel *pannel3 = _threeD_render_page->addPannel(QString());
		QToolBar *toolbar3 = toolbars.at(3);
		actions = toolbar3->actions();
		for (int j = 0; j < actions.size(); j++)
		{
			action = actions.at(j);
			pannel3->addLargeAction(action);
		}

		SARibbonPannel *pannel1 = _threeD_render_page->addPannel(QString());
		QToolBar *toolbar1 = toolbars.at(1);
		actions = toolbar1->actions();
		QList<QAction *> realActions;
		QList<QWidget *> comboxs;
		QWidget *widget{};
		for (int j = 0; j < actions.size(); j++)
		{
			action = actions.at(j);
			if (action->objectName() == "QComboBox")
			{
				widget = toolbar1->widgetForAction(action);
				widget->setVisible(true);
				comboxs.append(widget);
			}
			else if (action->objectName() == "QAction")
				realActions.append(action);
		}
		if (realActions.size() != 4 && comboxs.size() != 3)
			return;
		pannel1->addLargeAction(realActions.at(0));
		pannel1->addLargeAction(realActions.at(1));
		pannel1->addLargeAction(realActions.at(2));
		pannel1->addLargeAction(realActions.at(3));
		pannel1->addSmallWidget(comboxs.at(0));
		pannel1->addSmallWidget(comboxs.at(1));
		pannel1->addLargeWidget(comboxs.at(2));

		// modify
		/*
		int index = ribbon->tabIndex(_threeD_render_page);
		ribbon->setCurrentIndex(index);
		ribbon->onCurrentRibbonTabChanged(index);
		*/

		ribbon->raiseCategory(_threeD_render_page);
	}

	void SubWindowManager::remove3dRenderPage()
	{
		if (_threeD_render_page == nullptr)
			return;
		auto pannels = _threeD_render_page->pannelList();
		for (SARibbonPannel *pannel : pannels)
		{
			_threeD_render_page->removePannel(pannel);
			pannel = nullptr;
		}

		SARibbonBar *ribbon = _mainWindow->ribbonBar();
		ribbon->removeCategory(_threeD_render_page);
		_threeD_render_page = nullptr;

		ribbon->setCurrentIndex(4);
		// ribbon->onCurrentRibbonTabChanged(4);
	}

	void SubWindowManager::updatePreActors()
	{
		updatePreGeometryActor();
		updatePreMeshActor();
	}
	void SubWindowManager::updatePreMeshActor()
	{
		if (_preWindow.second != nullptr)
		{
			emit _preWindow.second->updateMeshActorSig();
		}
	}
	void SubWindowManager::updatePreGeometryActor()
	{
		if (_preWindow.second != nullptr)
		{
			_preWindow.second->updateGeometryActor();
		}
	}
	void SubWindowManager::closePreWindow()
	{
		_preWindow.first = nullptr;
		_preWindow.second = nullptr;
		_mainWindow->getUi()->actionPre_Window->setEnabled(true);

		emit _mainWindow->updateGeometryTreeSig();
		emit _mainWindow->updateMeshTreeSig();
		emit _mainWindow->updateSetTreeSig();
		emit _mainWindow->updatePhysicsTreeSignal();
		emit _mainWindow->updateProperty(nullptr);
		emit _mainWindow->updateActionStatesSig();

		//		_signalHander->clearData();
	}
	void SubWindowManager::openPostWindow(Post::PostWindowBase *pw)
	{
		if (pw == nullptr)
		{
			assert(0);
			return;
		}
		QMdiSubWindow *sw = nullptr;
		QList<Post::PostWindowBase *> pwl = _postWindow.values();
		if (pwl.contains(pw))
		{
			sw = _postWindow.key(pw);
		}
		else
		{
			sw = _mdiArea->addSubWindow(pw);
			_mdiArea->cascadeSubWindows();
			_postWindow[sw] = pw;
		}
		if (sw != nullptr)
		{
			sw->activateWindow();
			sw->showMaximized();
			sw->setFocus();
		}
		Post::PostProcessPy::init(_mainWindow, _mainWindow->getControlPanel()->getPostTreeWidget());
		Py::PythonAgent::getInstance()->unLock();
		emit _mainWindow->updateActionStatesSig();
	}

	void SubWindowManager::openRealTimeWindow(Post::RealTimeWindowBase *pw)
	{
		QMdiSubWindow *sw = nullptr;
		QList<Post::RealTimeWindowBase *> pwl = _realTimeWindow.values();
		if (pwl.contains(pw))
		{
			sw = _realTimeWindow.key(pw);
		}
		else
		{
			sw = _mdiArea->addSubWindow(pw);
			_realTimeWindow[sw] = pw;
		}
		if (sw != nullptr)
		{
			sw->showMaximized();
			sw->setFocus();
		}
		emit _mainWindow->updateActionStatesSig();
	}
	ModuleBase::GraphWindowBase *SubWindowManager::getCurrentWindow()
	{
		QMdiSubWindow *activeWindow = _mdiArea->activeSubWindow();
		if (activeWindow == nullptr)
			return nullptr;
		else if (activeWindow == _preWindow.first)
			return _preWindow.second;
		else if (_postWindow.contains(activeWindow))
			return _postWindow[activeWindow];
		else
			return nullptr;
	}
	Post::PostWindowBase *SubWindowManager::getPostWindowByIDType(int id, int type)
	{
		QList<Post::PostWindowBase *> pwl = _postWindow.values();
		for (int i = 0; i < pwl.size(); ++i)
		{
			Post::PostWindowBase *w = pwl.at(i);
			if (w == nullptr)
				continue;
			int wid = w->getID();
			int wtype = (int)w->getPostWindowType();
			if (wid == id && wtype == type)
				return w;
		}
		return nullptr;
	}
	void SubWindowManager::removeToolBars()
	{
		const int n = _currentToolBars.size();
		for (int i = 0; i < n; ++i)
		{
			QToolBar *t = _currentToolBars.at(i);
			_mainWindow->removeToolBar(t);
		}
		_currentToolBars.clear();
	}
	void SubWindowManager::showPostWindowInfo(int id, int type)
	{
		Post::PostWindowBase *pwb = getPostWindowByIDType(id, type);
		if (pwb == nullptr)
			return;
		// 		QWidget* ptree = pwb->getTreeWidget();
		// 		QWidget* pprop = pwb->getPropWidget();
		QMdiSubWindow *subWin = _postWindow.key(pwb);
		if (subWin != nullptr)
		{
			subWin->showMaximized();
			subWin->setFocus();
		}
		//_controlPanel->updatePostWidget(ptree, pprop);
		if (type == 2)
			_controlPanel->updatePostWidget();

		QList<QToolBar *> toolBars = pwb->getToolBarList();
		if (_mainWindow->isUseRibbon())
		{
			if (_threeD_render_page)
				return;
			add3dRenderPage(toolBars);
		}
		else
		{
			this->removeToolBars();
			_mainWindow->addToolBarBreak();
			for (int i = 0; i < toolBars.size(); ++i)
			{
				_mainWindow->addToolBar(toolBars.at(i));
				(toolBars.at(i))->show();
				_currentToolBars.append(toolBars.at(i));
			}
		}
	}
	void SubWindowManager::closePostWindow(Post::PostWindowBase *w)
	{
		QMdiSubWindow *subw = _postWindow.key(w);
		if (subw == nullptr)
			return;
		Post::PostWindowType type = w->getPostWindowType();
		removeToolBars();
		if (type == Post::PostWindowType::D3 && _mainWindow->isUseRibbon()) //使用ribbon，并且关闭的是3维窗口
			remove3dRenderPage();

		if (_controlPanel != nullptr)
			_controlPanel->updatePostWidget(nullptr, nullptr);
		emit _mainWindow->updateProperty(nullptr);

		_postWindow.remove(subw);
		QList<Post::PostWindowBase *> postsubwlist = _postWindow.values();
		if (_preWindow.first != nullptr)
			showPreWindow();
		else if (postsubwlist.size() > 0)
		{
			Post::PostWindowBase *w = postsubwlist.at(0);
			int id = w->getID();
			Post::PostWindowType type = w->getPostWindowType();
			showPostWindowInfo(id, type);
		}
		emit _mainWindow->updateActionStatesSig();
	}
	void SubWindowManager::showPreWindow()
	{
		removeToolBars();
		if (_controlPanel == nullptr)
			return;
		_controlPanel->updatePostWidget(nullptr, nullptr);
		if (_preWindow.first != nullptr)
		{
			_preWindow.first->showMaximized();
			_preWindow.first->setFocus();
		}
	}
	void SubWindowManager::showRealTimeWindow(QMdiSubWindow *w)
	{
		removeToolBars();
		_controlPanel->updatePostWidget(nullptr, nullptr);
		if (_realTimeWindow.contains(w))
		{
			w->showMaximized();
			w->setFocus();
		}
	}
	void SubWindowManager::subWindowActived(QMdiSubWindow *sw)
	{
		if (sw == nullptr)
			return;
		if (sw == _preWindow.first)
			showPreWindow();
		else if (_realTimeWindow.contains(sw))
			showRealTimeWindow(sw);
		else if (_postWindow.contains(sw))
		{
			Post::PostWindowBase *pwb = _postWindow.value(sw);
			int id = pwb->getID();
			Post::PostWindowType type = pwb->getPostWindowType();
			showPostWindowInfo(id, type);
		}
		emit _mainWindow->updateActionStatesSig();
	}
	void SubWindowManager::closeRealTimeWindow(Post::RealTimeWindowBase *w)
	{
		QMdiSubWindow *subw = _realTimeWindow.key(w);
		if (subw == nullptr)
			return;
		_realTimeWindow.remove(subw);
		if (_preWindow.first != nullptr)
			showPreWindow();
		emit _mainWindow->updateActionStatesSig();
	}
	void SubWindowManager::setView(QString view)
	{
		if (_c3fdRenderWindow.second != nullptr)
		{
			_c3fdRenderWindow.second->setView(view);
		}
		/*qDebug() << view;
		ModuleBase::GraphWindowBase* gwb = getCurrentWindow();
		if (gwb == nullptr) return;
		gwb->setView(view);*/
		ModuleBase::GraphWindowBase *gwb = getCurrentWindow();
		if (gwb == nullptr)
			return;
		const int id = gwb->getID();
		QString win = gwb->getStringGraphWindowType();
		qDebug() << win;
		QString pycode = QString("MainWindow.setView(%1,\"%2\",\"%3\")").arg(id).arg(win).arg(view);
		qDebug() << pycode;
		Py::PythonAgent::getInstance()->submit(pycode);
		// gwb->setView(view);
	}

	MainWidget::PreWindow *SubWindowManager::getPreWindow()
	{
		return _preWindow.second;
	}

	bool SubWindowManager::isPreWindowOpened()
	{
		if (_preWindow.first == nullptr || _preWindow.second == nullptr)
			return false;
		return true;
	}
	bool SubWindowManager::isPostWindowOpened()
	{
		if (_postWindow.size() > 0)
			return true;
		return false;
	}

	bool SubWindowManager::isPostWindowOpening()
	{
		QMdiSubWindow *activeWindow = _mdiArea->activeSubWindow();
		if (activeWindow == nullptr)
			return false;
		else if (_postWindow.contains(activeWindow))
			return true;
		else
			return false;
	}

	bool SubWindowManager::isC3fdWindowOpened()
	{
		if (_c3fdRenderWindow.first == nullptr || _c3fdRenderWindow.second == nullptr)
			return false;
		return true;
	}

	void SubWindowManager::saveImage(QString fileName, int winType, Post::PostWindowBase *winhandle, int w, int h)
	{
		if (winType == 0)
		{
			MainWidget::PreWindow *preW = _preWindow.second;
			if (preW == nullptr)
				return;
			preW->saveImage(fileName, w, h, false);
		}
		else if (winType == 1)
		{
			if (_postWindow.key(winhandle) == nullptr)
				return;
			winhandle->saveImage(fileName, w, h, false);
		}
	}

	void SubWindowManager::saveImage(int w, int h, QString f)
	{
		ModuleBase::GraphWindowBase *gw = this->getCurrentWindow();
		if (gw == nullptr)
		{
			_mainWindow->printMessage(Common::Message::Error, tr("No GraphWindow opened!"));
			return;
		}
		QString type = gw->getStringGraphWindowType();
		const int id = gw->getID();
		QString pycode = QString("MainWindow.saveImage(%1,%2,%3,\"%4\",\"%5\")").arg(w).arg(h).arg(id).arg(type).arg(f);
		qDebug() << pycode;
		Py::PythonAgent::getInstance()->submit(pycode);
	}

	void SubWindowManager::saveImage(int w, int h, int id, QString winType, QString file)
	{
		ModuleBase::GraphWindowBase *win = this->getWindowByTypeID(winType, id);
		if (win == nullptr)
			return;
		win->saveImage(file, w, h, false);
		Py::PythonAgent::getInstance()->unLock();
	}
	void SubWindowManager::setView(int id, QString winType, QString view) // liu
	{
		ModuleBase::GraphWindowBase *gwb = getWindowByTypeID(winType, id);
		if (gwb == nullptr)
			return;
		gwb->setView(view);
		Py::PythonAgent::getInstance()->unLock();
	}
	void SubWindowManager::setViewValue(int id, QString win, int x1, int x2, int x3, int y1, int y2, int y3, int z1, int z2, int z3)
	{
		ModuleBase::GraphWindowBase *gwb = getWindowByTypeID(win, id);
		if (gwb == nullptr)
			return;
		gwb->setViewValue(x1, x2, x3, y1, y2, y3, z1, z2, z3);
		Py::PythonAgent::getInstance()->unLock();
	}
	/*void SubWindowManager::setViewPreValue(int id, QString win, int x1, int x2, int x3, int y1, int y2, int y3, int z1, int z2, int z3)
	{
	ModuleBase::GraphWindowBase* gwb = getCurrentWindow();
	if (gwb == nullptr) return;
	gwb->setViewPreValue(x1, x2, x3, y1, y2, y3, z1, z2, z3);
	}*/
	void SubWindowManager::reTranslate()
	{
		MainWidget::PreWindow *prew = _preWindow.second;
		if (prew != nullptr)
			prew->reTranslate();

		int n = 0;
		QList<Post::PostWindowBase *> postWinList = _postWindow.values();
		n = postWinList.size();
		for (int i = 0; i < n; ++i)
		{
			Post::PostWindowBase *pw = postWinList.at(i);
			if (pw != nullptr)
				pw->reTranslate();
		}
		QList<Post::RealTimeWindowBase *> rwList = _realTimeWindow.values();
		n = rwList.size();
		for (int i = 0; i < n; ++i)
		{
			Post::RealTimeWindowBase *rw = rwList.at(i);
			if (rw != nullptr)
				rw->reTranslate();
		}

		if (_threeD_render_page != nullptr)
			_threeD_render_page->setWindowTitle(tr("3D Render"));
	}

	void SubWindowManager::closeGeometryWindow()
	{
		_geometryWindow.first = nullptr;
		_geometryWindow.second = nullptr;

		_signalHander->clearData(false);
		emit _mainWindow->updateGeometryTreeSig();
		emit _mainWindow->updateMeshTreeSig();
		emit _mainWindow->updatePhysicsTreeSignal();
		emit _mainWindow->updateProperty(nullptr);
	}

	GenerateGeometry::GenerateGeometryWidget *SubWindowManager::getGeometryWindow()
	{
		if (!_geometryWindow.second)
		{
			openGeometryWindow();
		}
		return _geometryWindow.second;
	}

	void SubWindowManager::closeAllSubWindow()
	{
		//		_mdiArea->setViewMode(QMdiArea::SubWindowView);
		//		_mdiArea->setTabPosition(QTabWidget::North);

		MainWidget::PreWindow *pre = _preWindow.second;
		if (pre != nullptr)
		{
			_preWindow.first->close();
			_mdiArea->removeSubWindow(pre);
			pre->close();
			delete pre;
		}

		QList<Post::PostWindowBase *> postlist = _postWindow.values();
		for (int i = 0; i < postlist.size(); ++i)
		{
			Post::PostWindowBase *p = postlist.at(i);
			_postWindow.key(p)->close();
			_mdiArea->removeSubWindow(p);
			p->close();
			delete p;
		}

		QList<Post::RealTimeWindowBase *> realtimelist = _realTimeWindow.values();
		for (int i = 0; i < realtimelist.size(); ++i)
		{
			Post::RealTimeWindowBase *r = realtimelist.at(i);
			_realTimeWindow.key(r)->close();
			_mdiArea->removeSubWindow(r);
			r->close();
			delete r;
		}
	}

	ModuleBase::GraphWindowBase *SubWindowManager::getWindowByTypeID(QString type, int id)
	{
		ModuleBase::GraphWindowBase *g = nullptr;
		type = type.toLower();
		if (type == "prewindow")
		{
			g = _preWindow.second;
		}
		else if (type == "realtime")
		{
			QList<Post::RealTimeWindowBase *> wlist = _realTimeWindow.values();
			for (int i = 0; i < wlist.size(); ++i)
			{
				ModuleBase::GraphWindowBase *t = wlist.at(i);
				const int d = t->getID();
				if (id == d)
				{
					g = t;
					break;
				}
			}
		}
		else if (type == "post2d")
		{
			QList<Post::PostWindowBase *> wlist = _postWindow.values();
			for (int i = 0; i < wlist.size(); ++i)
			{
				ModuleBase::GraphWindowBase *t = wlist.at(i);
				const int d = t->getID();
				if (id == d && t->getGraphWindowType() == ModuleBase::Post2D)
				{
					g = t;
					break;
				}
			}
		}
		else if (type == "post3d")
		{
			QList<Post::PostWindowBase *> wlist = _postWindow.values();
			for (int i = 0; i < wlist.size(); ++i)
			{
				ModuleBase::GraphWindowBase *t = wlist.at(i);
				const int d = t->getID();
				if (id == d && t->getGraphWindowType() == ModuleBase::Post3D)
				{
					g = t;
					break;
				}
			}
		}
		return g;
	}

	void SubWindowManager::setIcon(QString icon)
	{
		if (_preWindow.first != nullptr)
			_preWindow.first->setWindowIcon(QIcon(icon));
		QList<QMdiSubWindow *> subw = _postWindow.keys() + _realTimeWindow.keys();
		for (auto w : subw)
		{
			w->setWindowIcon(QIcon(icon));
		}
	}

}
